package com.kk.controller;

import com.kk.Entry.Dept;
import com.kk.Entry.R;
import com.kk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping
    public R<List<Dept>> getDept() {
        List<Dept> list = deptService.SelectDept();
        if (list.size() > 0) {
            return R.success(list);
        }
        return R.fail("无数据");
    }


    @DeleteMapping("{id}")
    public R<String> delDep(@PathVariable Integer id) {
        int i = deptService.delDep(id);
        if (i > 0) {
            return R.success("删除成功");
        }

        return R.fail("删除失败");
    }

    @GetMapping("{id}")
   public R<Dept> selectById(@PathVariable Integer id){
       Dept dept= deptService.selectById(id);
       return R.success(dept);
    }

    @PutMapping
    public R<String> updateEmp(@RequestBody @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Dept dept) {
        int i = deptService.update(dept);
        if (i > 0) {
            return R.success("更改成功");
        }

        return R.fail("更改失败");
    }

    @PostMapping
    public R<String> addDept(@RequestBody @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Dept dept) {
       int i = deptService.addDept(dept);
        if (i > 0) {
            return R.success("增加成功");
        }

        return R.fail("增加失败");
    }


}
